#!/bin/sh

if [ $# -ne 5 ]; then
    echo "Usage: $0 <oss_log_path> <mysql_username> <mysql_password> <mysql_host> <mysql_port>"
    exit 1
fi

OSS_DATABASE=oss
OSS_LOG_DIR=`readlink -f $1`
MYSQL_USERNAME=$2
MYSQL_PASSWORD=$3
MYSQL_HOST=$4
MYSQL_PORT=$5
WORK_DIR=/tmp/oss_import_`date '+%s'`
PWD=`pwd`

if [ ! -d $OSS_LOG_DIR ]; then
    echo "$OSS_LOG_DIR does not exist!"
    exit 1
fi

if [ -d $WORK_DIR ]; then
    echo "$WORK_DIR already exists!"
    exit 1
fi

mkdir $WORK_DIR
cd $WORK_DIR

for file in `ls $OSS_LOG_DIR/*oss*.log`; do
    echo "splitting oss log file: $file..."
    awk -F'|' '{print >> $1".csv"}' $file
    echo "oss log file: $file has been split."
done

for file in `ls *.csv`; do
    echo "removing prefix of $file..."
    rm -f tmp.file
    cut -d'|' -f2- $file > tmp.file
    mv tmp.file $file
    echo "prefix of $file has been removed."
done

for file in `ls *.csv`; do
    OSS_TABLE=`basename $file .csv`
    echo "importing $file into database: $OSS_DATABASE, table: $OSS_TABLE..."
    mysql --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD --host=$MYSQL_HOST --port=$MYSQL_PORT --execute="LOAD DATA LOCAL INFILE '$file' INTO TABLE $OSS_TABLE FIELDS TERMINATED BY '|'; SHOW WARNINGS;" $OSS_DATABASE
    echo "$file imported."
done

cd $PWD
rm -rf $WORK_DIR
echo "all work done, directory $WORK_DIR deleted."
